﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InspectionMAC.SystemThread
{
    class DiskSpaceThread
    {
        public static bool DiskSpaceThread_Flowrec = false;
        public static Thread DiskSpaceFlow_Thread = null;
        string outImgPath = Application.StartupPath + "\\out\\";
        static string rootPath = Application.StartupPath.Split(':')[0];
        public static void CheckDiskSpace_Thread()
        {
            DiskSpaceThread_Flowrec = true;
            DiskSpaceFlow_Thread = new Thread(DiskSpaceThreadFlow);
            DiskSpaceFlow_Thread.IsBackground = true;
            DiskSpaceFlow_Thread.Start();
        }

        private static void DiskSpaceThreadFlow()
        {
            while (DiskSpaceThread_Flowrec)
            {
                Thread.Sleep(1000);

                long temp_Total = GetHardDiskSpace(rootPath);
                long temp_Remaining = GetHardDiskFreeSpace(rootPath);
                float temp_Total_GB = temp_Total / GB;
                float temp_Remaining_GB = temp_Remaining / GB;
                Console.WriteLine("磁盘:{0} 容量:{1}GB 剩余:{2}GB 剩余比:{3}", rootPath, temp_Total_GB, temp_Remaining_GB, temp_Remaining_GB / temp_Total_GB);
            }

        }

        static int GB = 1024 * 1024 * 1024;//定义GB的计算常量
        static int MB = 1024 * 1024;//定义MB的计算常量
        static int KB = 1024;//定义KB的计算常量
        /// <summary>
        /// 超过当前磁盘容量85%  清理图片文件夹  
        /// </summary>
        /// <param name="remainCount">剩余文件夹个数</param>
        public void ClearOutImg(int remainCount)
        {
            string outImgPath = Application.StartupPath + "\\out\\";
            string rootPath = Application.StartupPath.Split(':')[0];
            //Console.WriteLine(rootPath);
            long temp_Total = GetHardDiskSpace(rootPath);
            long temp_Remaining = GetHardDiskFreeSpace(rootPath);
            float temp_Total_GB = temp_Total / GB;
            float temp_Remaining_GB = temp_Remaining / GB;
            //Console.WriteLine(temp_Total_GB);
            //Console.WriteLine(temp_Remaining_GB);
            //Console.WriteLine(temp_Remaining_GB / temp_Total_GB);
            if (temp_Remaining_GB / temp_Total_GB <= 0.15f)
            {
                //Console.WriteLine("超出容量，清理图片文件夹");
                DirectoryInfo outDirInfo = new DirectoryInfo(outImgPath);
                DirectoryInfo[] outFileInfo = outDirInfo.GetDirectories();
                List<string> outFilesList = new List<string>();

                if (outFileInfo.Length < remainCount)
                    return;

                foreach (DirectoryInfo item in outFileInfo)
                {
                    //Console.WriteLine("输出图片文件夹：" + item.Name);
                    outFilesList.Add(item.Name);
                }

                List<int> removeIndexList = new List<int>();
                for (int i = 0; i < outFilesList.Count; i++)
                {
                    if (i < outFilesList.Count - remainCount)
                    {
                        removeIndexList.Add(i);
                    }
                }

                foreach (var item in removeIndexList)
                {
                    //Console.WriteLine("删除的文件夹：" + outFileInfo[item].ToString());
                    if (Directory.Exists(outImgPath + outFileInfo[item]))
                    {
                        Directory.Delete(outImgPath + outFileInfo[item], true);
                    }

                }
            }
            else
            {
                //Console.WriteLine("Diskstatus:{0}","OutOfRange");
            }
        }

        /// 获取指定驱动器的空间总大小(单位为B) 
        /// </summary> 
        /// <param name="str_HardDiskName">只需输入代表驱动器的字母即可 </param> 
        /// <returns> </returns> 
        public static long GetHardDiskSpace(string str_HardDiskName)
        {
            long totalSize = new long();
            str_HardDiskName = str_HardDiskName + ":\\";
            System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
            foreach (System.IO.DriveInfo drive in drives)
            {
                if (drive.Name == str_HardDiskName)
                {
                    totalSize = drive.TotalSize;
                }
            }
            return totalSize;
        }

        /// <summary> 
        /// 获取指定驱动器的剩余空间总大小(单位为B) 
        /// </summary> 
        /// <param name="str_HardDiskName">只需输入代表驱动器的字母即可 </param> 
        /// <returns> </returns> 
        public static long GetHardDiskFreeSpace(string str_HardDiskName)
        {
            long freeSpace = new long();
            str_HardDiskName = str_HardDiskName + ":\\";
            System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
            foreach (System.IO.DriveInfo drive in drives)
            {
                if (drive.Name == str_HardDiskName)
                {
                    freeSpace = drive.TotalFreeSpace;
                }
            }
            return freeSpace;
        }
    }
}
